import React, { useState } from 'react';
import { useRequest } from 'ahooks';
import axios from 'axios';

function getUsername(userId) {
  console.log('🚀 ~ file: useRequest.jsx ~ line 5 ~ getUsername ~ userId', userId);
  return axios.get('/getList');
}
export default function UseRequest() {
  const [status, setStatus] = useState(Date.now());
  const { run, loading, data, refresh, params } = useRequest(getUsername, {
    defaultParams: ['1'],
    refreshDeps: [status],
    // pollingInterval: 10000, // 轮询间隔时间
    onBefore: () => console.log('onBefore'),
    onSuccess: (result, params) => console.log('onSuccess', result, params),
    onFinally: () => console.log('onFinally'),
    // debounceWait: 300, // 防抖时间
    // throttleWait: 300, // 节流时间
    retryCount: 3, // 重试次数
    // manual: true,
  });
  console.log(data);
  return (
    <div>
      <p>data =&gt; {!loading && data.data.data.records.map(item => item.pointName).join(',')}</p>
      <p>params =&gt; {params}</p>
      <p>isLoading =&gt; {loading}</p>
      <button type="button" onClick={() => run()}>
        run
      </button>
      <button type="button" onClick={() => refresh()}>
        刷新
      </button>
      <button type="button" onClick={() => setStatus(Date.now())}>
        切换用户
      </button>
    </div>
  );
}
